package com.otod.pm.prj.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.otod.base.json.JsonFailure;
import com.otod.base.json.JsonSuccess;
import com.otod.commons.entity.Page;
import com.otod.commons.utils.BeanUtils;
import com.otod.commons.utils.BizCodeUtil;
import com.otod.commons.utils.StringUtils;
import com.otod.commons.web.BaseController;
import com.otod.pm.prj.bo.TpBo;
import com.otod.pm.prj.service.DetailService;
import com.otod.pm.prj.service.ProjectService;
import com.otod.pm.prj.service.StepService;
import com.otod.pm.prj.service.TeamService;
import com.otod.pm.prj.service.TpService;
import com.otod.pm.prj.vo.DetailVo;
import com.otod.pm.prj.vo.TpVo;

/**
 * 项目管理
 * @author yiChen
 */


@Controller
@RequestMapping("pm/tp")
public class TpController extends BaseController {
	
	@Autowired
	private ProjectService projectService;
	@Autowired
	private TpService tpService;
	@Autowired
	private DetailService detailService;
	@Autowired
	private TeamService teamService;
	@Autowired
	private StepService stepService;
	
	private static Logger logger = LoggerFactory.getLogger(TpController.class);
	
	/**
	 * 根据项目ID获取角色信息
	 * @param id
	 * @return
	 */
	@RequestMapping(value="getByTpName",method=RequestMethod.POST)
	@ResponseBody
	public TpVo getByTpName(String tpName) {
		TpVo vo = tpService.getByTpName(tpName);
		return vo;
   }
	
	
	
	
	
	
	
	/**
	 * 保存项目人员信息
	 * @param vo
	 * @return
	 */
	@RequestMapping(value="save",method=RequestMethod.POST)
	@ResponseBody
	public TpVo save(TpVo vo) {
		if(StringUtils.isEmpty(vo.getUuid())){			//新增
			return tpService.addTp(vo);
		}else{											//修改
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("uuid", vo.getUuid());
			map.put("tpName", vo.getTpName());
			map.put("prjType", vo.getPrjType());
			
			tpService.updateTp(map);
			return vo;
		}
	}
	
	
	/********************************************longjinwei********************************/
	
	/**
	 * 分页查询 模板信息   
	 * @param request
	 * @return
	 */
	@RequestMapping(value="queryTp",method=RequestMethod.GET)
	@ResponseBody
	public Page queryPage(HttpServletRequest request){
		Map params = super.getParamMapFromRequest(request);
		Page page = new Page(request);		
		page =  tpService.queryForList(page, params);

		//组装BO
		List<TpVo> list = page.getAaData();
		List<TpBo> listBo = new ArrayList<TpBo>(); 
		TpBo tpBo;
		for (TpVo tp : list) {
			tpBo = new TpBo();
			BeanUtils.copyProperties(tp,tpBo);
			//调用通用类型转换方法
			BizCodeUtil.putCodeName("pmProjectType", "prjType", "prjTypeName", tpBo);
			listBo.add(tpBo);
		}
		page.setAaData(listBo);
		return page;
	}
	
	/**
	 * 删除模板 信息
	 * @param vo
	 * @return
	 */
	@RequestMapping(value="delTp",method=RequestMethod.GET)
	@ResponseBody
	public String delTp(String uuid){
		
		if(StringUtils.isEmpty(uuid)){
			return JsonFailure.factory("请选择要删除的项目");
		}		
		tpService.delTp(uuid);
		return JsonSuccess.factory("删除项目成功");
	}
	/**
	 * 通过项目的类别查询模板
	 * @param prjType
	 * @return
	 */
	@RequestMapping(value="queryTpPrjType",method=RequestMethod.GET)
	@ResponseBody
	public List<TpVo> queryTpPrjType(String prjType){
		logger.debug("prjType is {}",prjType );
		Map<String, Object> param = new HashMap<String,Object>();
		param.put("prjType", prjType);
		return tpService.queryForList(param);
	}
	
	
	
	
	/************************************处理修改pm_t_detail的基础方法**********************************************/
	
	
	public Map<String, Object> baseDetaIlVo(DetailVo vo){
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("uuid", vo.getUuid());
		map.put("prjId", vo.getPrjId());
		map.put("cfgVersion", vo.getCfgVersion());
		map.put("prjPlace", vo.getPrjPlace());
		map.put("contacts", vo.getContacts());
		map.put("addr", vo.getAddr());
		map.put("postcode", vo.getPostcode());
		map.put("telephone", vo.getTelephone());
		map.put("undertakeType", vo.getUndertakeType());
		map.put("undertakeUnit", vo.getUndertakeUnit());
		map.put("dateClmn1", vo.getDateClmn1());
		map.put("dateClmn2", vo.getDateClmn2());
		map.put("dateClmn3", vo.getDateClmn3());
		map.put("dateClmn4", vo.getDateClmn4());
		map.put("decClmn1", vo.getDateClmn1());
		map.put("decClmn2", vo.getDateClmn2());
		map.put("decClmn3", vo.getDateClmn3());
		map.put("decClmn4", vo.getDateClmn4());
		map.put("longClmn1", vo.getLongClmn1());
		map.put("longClmn2", vo.getLongClmn2());
		map.put("longClmn3", vo.getLongClmn3());
		map.put("longClmn4", vo.getLongClmn4());
		map.put("varClmn1", vo.getVarClmn1());
		map.put("varClmn2", vo.getVarClmn2());
		map.put("varClmn3", vo.getVarClmn3());
		map.put("varClmn4", vo.getVarClmn4());
		map.put("varClmn5", vo.getVarClmn5());
		map.put("varClmn6", vo.getVarClmn6());
		map.put("varClmn7", vo.getVarClmn7());
		map.put("varClmn8", vo.getVarClmn8());
		return  map;
	}
}
